package laba1;

import junit.framework.TestCase;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.NoSuchElementException;

/**
 * Created by Михаил on 07.04.14.
 */
public class MyLinkedDequeTest extends TestCase {
    MyLinkedDeque mld;
    @BeforeMethod
    public void setUp() throws Exception {
        mld = new MyLinkedDeque();
    }

    @AfterMethod
    public void tearDown() throws Exception {
        mld = null;
    }

    @Test
    public void testToString() throws Exception {
        int[] t = {5,4,3};
        mld = new MyLinkedDeque(t);
        assertEquals("testToString", "5 4 3 ",mld.toString());
    }

    @Test
    public void testClear() throws Exception {
        mld.clear();
        int[] t = {5,4,3};
        mld = new MyLinkedDeque(t);
        mld.clear();
        assertEquals("testClear", "", mld.toString());
    }

    @Test
    public void testAddFirst() throws Exception {
        String expected = "";
        int size = 100;
        for (int i = 0; i < size; i++) {
            mld.addFirst(i);
            expected = i + " " + expected;
        }
        assertEquals("testAddFirst", expected,mld.toString());
    }

    @Test
    public void testAddLast() throws Exception {
        String expected = "";
        int size = 100;
        for (int i = 0; i < size; i++) {
            mld.addLast(i);
            expected += i + " ";
        }
        assertEquals("testAddLast", expected,mld.toString());
    }

    @Test
    public void testPollFirst() throws Exception {
        assertNull("testPollFirst", mld.pollFirst());
        int[] p = {5,4,3};
        mld = new MyLinkedDeque(p);
        assertEquals("testPollFirst", 5,(int)mld.pollFirst());
        assertEquals("testPollFirst", 4,(int)mld.pollFirst());
        assertEquals("testPollFirst", 3,(int)mld.pollFirst());
    }

    @Test
    public void testPollLast() throws Exception {
        assertNull("testPollLast", mld.pollLast());
        int[] p = {5,4,3};
        mld = new MyLinkedDeque(p);
        assertEquals("testPollLast", 3,(int)mld.pollLast());
        assertEquals("testPollLast", 4,(int)mld.pollLast());
        assertEquals("testPollLast", 5,(int)mld.pollLast());
    }

    @Test
    public void testGetFirst() throws Exception {
        int[] p = {5,4,3};
        mld = new MyLinkedDeque(p);
        assertEquals("testGetFirst", 5,(int)mld.getFirst());
        assertEquals("testGetFirst", 5,(int)mld.getFirst());
        assertEquals("testGetFirst", 5,(int)mld.getFirst());
    }

    @Test(expectedExceptions = NoSuchElementException.class)
    public void testGetFirstErrors() throws Exception {
        boolean success = false;
        try {
            mld.getFirst();
        } catch (NoSuchElementException e) {
            success = true;
        }
        assert success;
    }

    @Test
    public void testGetLast() throws Exception {
        int[] p = {5,4,3};
        mld = new MyLinkedDeque(p);
        assertEquals("testGetLast", 3,(int)mld.getLast());
        assertEquals("testGetLast", 3,(int)mld.getLast());
        assertEquals("testGetLast", 3,(int)mld.getLast());
    }

    @Test(expectedExceptions = NoSuchElementException.class)
    public void testGetLastErrors() throws Exception {
        boolean success = false;
        try {
            mld.getLast();
        } catch (NoSuchElementException e) {
            success = true;
        }
        assert success;
    }

    @Test
    public void testPeekFirst() throws Exception {
        assertNull("testPeekFirst", mld.peekFirst());
        int[] p = {5,4,3};
        mld = new MyLinkedDeque(p);
        assertEquals("testPeekFirst", 5,(int)mld.peekFirst());
        assertEquals("testPeekFirst", 5,(int)mld.peekFirst());
        assertEquals("testPeekFirst", 5,(int)mld.peekFirst());
    }

    @Test
    public void testPeekLast() throws Exception {
        assertNull("testPeekLast", mld.peekLast());
        int[] p = {5,4,3};
        mld = new MyLinkedDeque(p);
        assertEquals("testPeekLast", 3,(int)mld.peekLast());
        assertEquals("testPeekLast", 3,(int)mld.peekLast());
        assertEquals("testPeekLast", 3,(int)mld.peekLast());
    }

    @Test
    public void testSize() throws Exception {
        assertEquals("testSize", 0, mld.size());
        int[] p = {5,4,3};
        mld = new MyLinkedDeque(p);
        assertEquals("testSize", 3, mld.size());
    }

    @Test
    public void testIsEmpty() throws Exception {
        assertTrue("testIsEmpty", mld.isEmpty());
        mld = new MyLinkedDeque(new int[] {5});
        assertFalse("testIsEmpty", mld.isEmpty());
    }
}
